package com.qf.entity;

import cn.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 *   `id` int(11) NOT NULL AUTO_INCREMENT,
 *   `name` varchar(50) DEFAULT NULL,
 *   `author` varchar(50) DEFAULT NULL,
 *   `publish` varchar(50) DEFAULT NULL,
 *   `sort` int(50) DEFAULT NULL,
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {

    private Integer id;
    private String name;
    private String author;
    private String publish;
    private Integer sort;
    public static Book randomBook(){
        Book book = new Book();
        final String name = RandomUtil.randomString(20);
        final String author = RandomUtil.randomString(10);
        final String publish = RandomUtil.randomString(25);
        final int sort = RandomUtil.randomInt(0,1000000);
        book.setName(name);
        book.setAuthor(author);
        book.setPublish(publish);
        book.setSort(sort);
        return book;
    }
    public static List<Book> randomBooks(int size){
        ArrayList<Book> books = new ArrayList<>();

        for (int i = 0; i < size; i++) {
            books.add(randomBook());
        }
        return books;

    }

    @Override
    public String toString() {
        return this.name+","+this.author+","+this.publish+","+this.sort;
    }
    public static Book csvToBook(String csv){
        final String[] split = csv.split(",");
        final Book book = new Book();
        book.setName(split[0]);
        book.setAuthor(split[1]);
        book.setPublish(split[2]);
        book.setSort( Integer.getInteger(split[3]));
        return book;
    }
}

